const http = require('http');
const request = require('request');
const mysqlUtil = require("./mysql-util");
// setInterval(getData,1000);  
function getData()  {  
    // var url = "http://hq.sinajs.cn/list=sh601857";
    var url = "http://qt.gtimg.cn/q=sz000858";
    // http.request({}); 
    http.get(url, function(res) {  
        var source = "";  
        res.on('data', function(data) { 
            console.log(data);
            source = data.toString();
        });  
        res.on('end', function() {
        	// console.log(source)
            // let TodayOpeningPrice = source.split(',')[1];
            // let YesterdayClosingPrice = source.split(',')[2];
            // let CurrentPrice = source.split(',')[3];
            // let DayHighPrice = source.split(',')[4];
            // let TodaylowestPrice = source.split(',')[5];
            // let BidPrice = source.split(',')[6];
            // let AuctionPrice = source.split(',')[7];
            // let StockNumber = source.split(',')[8];
            // let TransactionAmount = source.split(',')[9];
            // let NumberOfPurchases1 = source.split(',')[10];
            // let BuyOnePrice1 = source.split(',')[11];
            // let NumberOfPurchases2 = source.split(',')[12];
            // let BuyOnePrice2 = source.split(',')[13];
            // let NumberOfPurchases3 = source.split(',')[14];
            // let BuyOnePrice3 = source.split(',')[15];
            // let NumberOfPurchases4 = source.split(',')[16];
            // let BuyOnePrice4 = source.split(',')[17];
            // let NumberOfPurchases5 = source.split(',')[18];
            // let BuyOnePrice5 = source.split(',')[19];
            // let NumberOfShares1 = source.split(',')[20];
            // let SellingPrice1 = source.split(',')[21];
            // let NumberOfShares2 = source.split(',')[22];
            // let SellingPrice2 = source.split(',')[23];
            // let NumberOfShares3 = source.split(',')[24];
            // let SellingPrice3 = source.split(',')[25];
            // let NumberOfShares4 = source.split(',')[26];
            // let SellingPrice4 = source.split(',')[27];
            // let NumberOfShares5 = source.split(',')[28];
            // let SellingPrice5 = source.split(',')[29];
            // let Dateruqi = source.split(',')[30];
            // let Time = source.split(',')[31];
            // console.log(
            // 	"今日开盘价:"+TodayOpeningPrice+
            // 	"昨日收盘价:"+YesterdayClosingPrice+
            // 	"当前价格:"+CurrentPrice+
            // 	"今日最高价:"+DayHighPrice+
            // 	"今日最低价:"+TodaylowestPrice+
            // 	"竞买价:"+BidPrice+
            // 	"竞卖价:"+AuctionPrice+
            // 	"成交的股票数:"+StockNumber+
            // 	"成交金额:"+TransactionAmount+
            // 	"“买一”申请股数:"+NumberOfPurchases1+
            // 	"“买一”报价:"+BuyOnePrice1+
            // 	"“买二”申请股数:"+NumberOfPurchases2+
            // 	"“买二”报价:"+BuyOnePrice2+
            // 	"“买三”申请股数:"+NumberOfPurchases3+
            // 	"“买三”报价:"+BuyOnePrice3+
            // 	"“买四”申请股数:"+NumberOfPurchases4+
            // 	"“买四”报价:"+BuyOnePrice4+
            // 	"“买五”申请股数:"+NumberOfPurchases5+
            // 	"“买五”报价:"+BuyOnePrice5+
            // 	"“卖一”申请股数:"+NumberOfShares1+
            // 	"“卖一”报价:"+SellingPrice1+
            // 	"“卖二”申请股数:"+NumberOfShares2+
            // 	"“卖二”报价:"+SellingPrice2+
            // 	"“卖三”申请股数:"+NumberOfShares3+
            // 	"“卖三”报价:"+SellingPrice3+
            // 	"“卖四”申请股数:"+NumberOfShares4+
            // 	"“卖四”报价:"+SellingPrice4+
            // 	"“卖五”申请股数:"+NumberOfShares5+
            // 	"“卖五”报价:"+SellingPrice5+
            // 	"日期:"+Dateruqi+
            // 	"时间:"+Time
            // 	);

        })
    }).on('error', function() {  
        console.log("获取数据出现错误");  
    });
};
// getData()
//伪造用户代理
function randomUserAgent() {
  const userAgentList = [
    'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36',
    'Mozilla/5.0 (iPhone; CPU iPhone OS 9_1 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Version/9.0 Mobile/13B143 Safari/601.1',
    'Mozilla/5.0 (iPhone; CPU iPhone OS 9_1 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Version/9.0 Mobile/13B143 Safari/601.1',
    'Mozilla/5.0 (Linux; Android 5.0; SM-G900P Build/LRX21T) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Mobile Safari/537.36',
    'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Mobile Safari/537.36',
    'Mozilla/5.0 (Linux; Android 5.1.1; Nexus 6 Build/LYZ28E) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Mobile Safari/537.36',
    'Mozilla/5.0 (iPhone; CPU iPhone OS 10_3_2 like Mac OS X) AppleWebKit/603.2.4 (KHTML, like Gecko) Mobile/14F89;GameHelper',
    'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_5) AppleWebKit/603.2.4 (KHTML, like Gecko) Version/10.1.1 Safari/603.2.4',
    'Mozilla/5.0 (iPhone; CPU iPhone OS 10_0 like Mac OS X) AppleWebKit/602.1.38 (KHTML, like Gecko) Version/10.0 Mobile/14A300 Safari/602.1',
    'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36',
    'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.12; rv:46.0) Gecko/20100101 Firefox/46.0',
    'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:46.0) Gecko/20100101 Firefox/46.0',
    'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0)',
    'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0; Trident/4.0)',
    'Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)',
    'Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; Win64; x64; Trident/6.0)',
    'Mozilla/5.0 (Windows NT 6.3; Win64, x64; Trident/7.0; rv:11.0) like Gecko',
    'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.135 Safari/537.36 Edge/13.10586',
    'Mozilla/5.0 (iPad; CPU OS 10_0 like Mac OS X) AppleWebKit/602.1.38 (KHTML, like Gecko) Version/10.0 Mobile/14A300 Safari/602.1'
  ]
  const num = Math.floor(Math.random() * userAgentList.length)
  return userAgentList[num]
};

var options = {
    url: 'http://hq.sinajs.cn/list=sh601857',
    hostname: "hq.sinajs.cn",
    method: "GET",
    headers: {
        Accept: '*/*',
		Host: 'hq.sinajs.cn',
		Referer: 'http://finance.sina.com.cn/realstock/company/sh601857/nc.shtml',
        'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8',
        Connection: 'keep-alive',
        'Content-Type': 'application/x-www-form-urlencoded',
        'User-Agent': randomUserAgent(),
    },
};
let sqlParam = new Array();
function callback(error, response, body) {
    if (!error && response.statusCode == 200) {
        let source = body.toString();
        let TodayOpeningPrice = source.split(',')[1];
        let YesterdayClosingPrice = source.split(',')[2];
        let CurrentPrice = source.split(',')[3];
        let DayHighPrice = source.split(',')[4];
        let TodaylowestPrice = source.split(',')[5];
        let BidPrice = source.split(',')[6];
        let AuctionPrice = source.split(',')[7];
        let StockNumber = source.split(',')[8];
        let TransactionAmount = source.split(',')[9];
        let NumberOfPurchases1 = source.split(',')[10];
        let BuyOnePrice1 = source.split(',')[11];
        let NumberOfPurchases2 = source.split(',')[12];
        let BuyOnePrice2 = source.split(',')[13];
        let NumberOfPurchases3 = source.split(',')[14];
        let BuyOnePrice3 = source.split(',')[15];
        let NumberOfPurchases4 = source.split(',')[16];
        let BuyOnePrice4 = source.split(',')[17];
        let NumberOfPurchases5 = source.split(',')[18];
        let BuyOnePrice5 = source.split(',')[19];
        let NumberOfShares1 = source.split(',')[20];
        let SellingPrice1 = source.split(',')[21];
        let NumberOfShares2 = source.split(',')[22];
        let SellingPrice2 = source.split(',')[23];
        let NumberOfShares3 = source.split(',')[24];
        let SellingPrice3 = source.split(',')[25];
        let NumberOfShares4 = source.split(',')[26];
        let SellingPrice4 = source.split(',')[27];
        let NumberOfShares5 = source.split(',')[28];
        let SellingPrice5 = source.split(',')[29];
        let Dateruqi = source.split(',')[30];
        let Time = source.split(',')[31];
        sqlParam = source.split(',').splice(1,31);
        ceshi(sqlParam);
    };
};
setInterval(function(){
	request(options, callback);
},3000);
let ceshi = async function (data) {
    // console.log(data)
    // 获取连接
    let conn = await mysqlUtil();
    // 开启事物
    await new Promise((resolve, reject) => {
        conn.beginTransaction(err => {
            if (err) {
                reject(err);
            } else {
                resolve();
            }
        });
    });
    // 执行第一个 SQL 语句
    let result1 = await new Promise((resolve, reject) => {
        let sqlStr = "INSERT INTO time_sharing (TodayOpeningPrice, YesterdayClosingPrice,CurrentPrice,DayHighPrice,TodaylowestPrice,BidPrice,AuctionPrice,StockNumber,TransactionAmount,NumberOfPurchases1,BuyOnePrice1,NumberOfPurchases2,BuyOnePrice2,NumberOfPurchases3,BuyOnePrice3,NumberOfPurchases4,BuyOnePrice4,NumberOfPurchases5,BuyOnePrice5,NumberOfShares1,SellingPrice1,NumberOfShares2,SellingPrice2,NumberOfShares3,SellingPrice3,NumberOfShares4,SellingPrice4,NumberOfShares5,SellingPrice5,Dateruqi,Time) values (?, ?,?,?,?,?, ?,?,?,?,?, ?,?,?,?,?, ?,?,?,?,?, ?,?,?,?,?, ?,?,?,?,?)";//添加数据
        conn.query(sqlStr, data, function (err, ret) {
            if (err) { // 回滚之前的数据库操作, 直至碰到 beginTransaction
                return conn.rollback(() => {
                    resolve(err);
                });
            }
            resolve(ret);
        });
    });
    console.log(result1);
    // 关闭事务
    await new Promise((resolve, reject) => {
        conn.commit(err => {
            if (err) {
                reject(err);
            } else {
                resolve();
            }
        });
    });
　　 conn.release();　　
};